package top.forforever.srb.mq.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @create: 2023/6/9
 * @Description:
 * @FileName: MqConfirmCofnig
 * @自定义内容：
 */
@Component
public class MqConfirmConfig implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    private void init() {
        rabbitTemplate.setReturnCallback(this);
        rabbitTemplate.setConfirmCallback(this);
    }

    //消息到交换机之间的确认，一定会经过
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        //进行死讯队列，保证消息100%投递性
        System.out.println("消息发送确认");
    }

    //消息从交换机到队列之间，只有发送失败才回调
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("消息投递确认");
    }
}
